package com.validator;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckPhone extends FieldValidatorSupport {
		private boolean trim = true;
		@Override
		public void validate(Object o) throws ValidationException {
		//1. 获取字段的名字和值
		String getPhoneClass = getFieldName();
		String fieldValueValue = (String) getFieldValue(getPhoneClass, o);
		//2. 验证
		if (fieldValueValue == null)
			return;
		if (trim)
			fieldValueValue = fieldValueValue.trim();
		if (fieldValueValue.length() == 0) {
			return;
		}
		String reg ="^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
        //编译正则表达式
    	Pattern pattern = Pattern.compile(reg);
   	  	Matcher matcher = pattern.matcher(fieldValueValue);
   	  	// 字符串是否与正则表达式相匹配
   	  	boolean rs = matcher.matches();
   	  	if (rs == false) {
			addFieldError(getPhoneClass, o);
		}
	
	}
	
}
